<template>
  <div class="fullpage">
    <div v-if="!showForm">
      <h2>景点管理</h2>
      <div class="toolbar">
        <el-input v-model="keyword" placeholder="搜索景点名称" @keyup.enter="load" clearable />
        <el-button type="primary" @click="load">搜索</el-button>
        <el-button type="success" @click="showAddForm">新增景点</el-button>
      </div>
      <el-table :data="spots" style="width: 100%;">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="price" label="价格" width="100" />
        <el-table-column prop="stock" label="余量" width="100">
          <template #default="scope">
            {{ scope.row.stock ?? 0 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template #default="scope">
            <el-button size="small" @click="editSpot(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="delSpot(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="page"
        :page-size="size"
        :page-sizes="[5, 10, 20]"
        :total="total"
        layout="prev, pager, next, jumper, sizes"
        @current-change="load"
        @size-change="handleSizeChange"
        style="margin-top: 16px; text-align: right;"
      />
    </div>
    <div v-if="showForm">
      <div class="form-header">
        <h2>{{ editForm.id ? '编辑景点' : '新增景点' }}</h2>
        <el-button @click="backToList">返回列表</el-button>
      </div>
      <el-form :model="editForm" :rules="rules" ref="formRef" label-width="80px" style="width: 100%;">
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="editForm.description" type="textarea" :rows="3" autocomplete="off" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="editForm.price" :min="0" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="门票数量" prop="stock">
          <el-input-number v-model="editForm.stock" :min="1" style="width: 100%;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveSpot">保存</el-button>
          <el-button @click="backToList">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
const spots = ref([])
const page = ref(1)
const size = ref(10)
const total = ref(0)
const keyword = ref('')
const showForm = ref(false)
const editForm = ref({ id: null, name: '', description: '', price: 0, stock: 100 })
const formRef = ref()
const rules = {
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
  stock: [{ required: true, message: '请输入门票数量', trigger: 'blur' }]
}
const load = async () => {
  const res = await axios.get('/api/scenic-spots/page', { params: { page: page.value, size: size.value, keyword: keyword.value } })
  spots.value = res.data.data.records
  total.value = res.data.data.total
}
onMounted(load)
function showAddForm() {
  showForm.value = true
  editForm.value = { id: null, name: '', description: '', price: 0 }
}
function editSpot(s) {
  showForm.value = true
  editForm.value = { ...s }
}
function delSpot(id) {
  axios.delete('/api/scenic-spots/' + id)
    .then(() => { ElMessage.success('删除成功'); load() })
    .catch(e => {
      ElMessage.error(e.response?.data?.msg || '删除失败')
    })
}
function saveSpot() {
  formRef.value.validate(async valid => {
    if (!valid) return
    if (editForm.value.id) {
      await axios.put('/api/scenic-spots', editForm.value)
      ElMessage.success('修改成功')
    } else {
      const { id, ...data } = editForm.value
      await axios.post('/api/scenic-spots', data)
      ElMessage.success('新增成功')
    }
    backToList(); load()
  })
}
function backToList() {
  showForm.value = false
  editForm.value = { id: null, name: '', description: '', price: 0 }
  if (formRef.value) formRef.value.resetFields()
}
function handleSizeChange(val) {
  size.value = val
  page.value = 1
  load()
}
</script>
<style>
.fullpage {
  width: 100%;
  height: 100%;
  background: #f5f6fa;
  padding: 0;
  box-sizing: border-box;
}
.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style> 